package cn.iocoder.yudao.module.monitor.service.realtime;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.monitor.controller.admin.realtime.vo.*;
import cn.iocoder.yudao.module.monitor.dal.dataobject.realtime.RealTimeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.monitor.convert.realtime.RealTimeConvert;
import cn.iocoder.yudao.module.monitor.dal.mysql.realtime.RealTimeMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.monitor.enums.ErrorCodeConstants.*;

/**
 * 监控 Service 实现类
 *
 * @author 左达成
 */
@Service
@Validated
public class RealTimeServiceImpl implements RealTimeService {

    @Resource
    private RealTimeMapper realTimeMapper;

    @Override
    public Long createRealTime(RealTimeCreateReqVO createReqVO) {
        // 插入
        RealTimeDO realTime = RealTimeConvert.INSTANCE.convert(createReqVO);
        realTimeMapper.insert(realTime);
        // 返回
        return realTime.getId();
    }

    @Override
    public void updateRealTime(RealTimeUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateRealTimeExists(updateReqVO.getId());
        // 更新
        RealTimeDO updateObj = RealTimeConvert.INSTANCE.convert(updateReqVO);
        realTimeMapper.updateById(updateObj);
    }

    @Override
    public void deleteRealTime(Long id) {
        // 校验存在
        this.validateRealTimeExists(id);
        // 删除
        realTimeMapper.deleteById(id);
    }

    private void validateRealTimeExists(Long id) {
        if (realTimeMapper.selectById(id) == null) {
            throw exception(REAL_TIME_NOT_EXISTS);
        }
    }

    @Override
    public RealTimeDO getRealTime(Long id) {
        return realTimeMapper.selectById(id);
    }

    @Override
    public List<RealTimeDO> getRealTimeList(Collection<Long> ids) {
        return realTimeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<RealTimeDO> getRealTimePage(RealTimePageReqVO pageReqVO) {
        return realTimeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<RealTimeDO> getRealTimeList(RealTimeExportReqVO exportReqVO) {
        return realTimeMapper.selectList(exportReqVO);
    }

}
